package com.fingard.dsp.bank.directbank.allinpay02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

public class AllInPay02Transaction extends AllInPay02Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                ActSetItem actItem = getBankConfig().getActSetItem(p_req7516.actNum, "unionRecPay");
                if (actItem == null) {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo("未找到账号:[" + p_req7516.actNum + "]商户配置信息!");
                    retAts.writeEnd();
                    return retAts;
                }

                AllInPay02Header bankHeader = new AllInPay02Header();
                bankHeader.TRX_CODE = "300005";
                bankHeader.REQ_SN = getQryTransID();

                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeAgrmCommonHeader(xmlWriter, bankHeader, actItem);
                xmlWriter.writeStartElement("ETQREQ");
                xmlWriter.writeElementString("Q_DATE", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), actItem);

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String bankRetCode = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
                        if ("0000".equals(bankRetCode)) {
                            while (tmpBkReader.read("/AIPG/ETQRSP/ETDTLS/ETDTL")) {
                                tmpRetDetail.cur = p_req7516.actCur;
                                tmpRetDetail.actArea = p_req7516.actArea;
                                tmpRetDetail.actNum = p_req7516.actNum;

                                String transTime = tmpBkReader.readedXElement.getSingleText("POSTDATE");
                                tmpRetDetail.tranDate = Format.Date10Format.format(Format.DateTime14Format.parse(transTime));
                                tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                tmpRetDetail.tranTime = Format.DateTimeFormat.format(Format.DateTime14Format.parse(transTime));
                                String tranAmt = tmpBkReader.readedXElement.getSingleText("AMOUNT");
                                String cdFlag = tmpBkReader.readedXElement.getSingleText("CRDR");
                                if ("1".equals(cdFlag)) {
                                    cdFlag = "2";
                                } else if ("2".equals(cdFlag)) {
                                    if (tranAmt.startsWith("-")) {
                                        cdFlag = "2";
                                    } else {
                                        cdFlag = "1";
                                    }
                                }
                                tranAmt = tranAmt.replace("-", "");
                                tmpRetDetail.tranAmt = StringHelper.amountDivide100(tranAmt);
                                tmpRetDetail.cdFlag = cdFlag;
                                tmpRetDetail.askNumber = tmpBkReader.readedXElement.getSingleText("TRXID");

                                retAts.writeRetDetailWithoutRespCode(tmpRetDetail);
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG"));
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);

        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;

        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;

        return d_7516_HisTrans(tmpReq7516);
    }

    public int analysis7516Loop(String[] tmpStrRet, Req7516HisTrans p_req7516, RetAtsTrans retAts) throws Exception {
        int page = -1;
        RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String totalPageNum = tmpBkReader.getFirstText("/AIPG/ETQRSP/ETSUM/SUM");
                page = Integer.parseInt(totalPageNum);

                String bankRetCode = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
                if ("0000".equals(bankRetCode)) {
                    while (tmpBkReader.read("/AIPG/ETQRSP/ETDTLS/ETDTL")) {
                        tmpRetDetail.cur = p_req7516.actCur;
                        tmpRetDetail.actArea = p_req7516.actArea;
                        tmpRetDetail.actNum = p_req7516.actNum;

                        String transTime = tmpBkReader.readedXElement.getSingleText("POSTDATE");
                        tmpRetDetail.tranDate = Format.Date10Format.format(Format.DateTime14Format.parse(transTime));
                        tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                        tmpRetDetail.tranTime = Format.DateTimeFormat.format(Format.DateTime14Format.parse(transTime));
                        String tranAmt = tmpBkReader.readedXElement.getSingleText("AMOUNT").replace("-", "");
                        tmpRetDetail.tranAmt = StringHelper.amountDivide100(tranAmt);

                        String cdFlag = tmpBkReader.readedXElement.getSingleText("CRDR");
                        if ("1".equals(cdFlag)) {
                            cdFlag = "2";
                        } else if ("2".equals(cdFlag)) {
                            cdFlag = "1";
                        }
                        tmpRetDetail.cdFlag = cdFlag;
                        tmpRetDetail.askNumber = tmpBkReader.readedXElement.getSingleText("TRXID");

                        retAts.writeRetDetailWithoutRespCode(tmpRetDetail);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG"));
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpSbMsg.toString());
            }
        } else {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(tmpStrRet[0].toString());
        }

        return page;
    }
}
